import dayjs from 'dayjs';
import { FormCard } from '@/model/types/formCard';
import { DataTypeMap } from '@/model/objectMap/dataTypeMap';

export const IsUserDept = (formCards: FormCard[]) => {
  const list = [
    DataTypeMap.SingleParticipant,
    DataTypeMap.MultiParticipant,
    DataTypeMap.SingleDepartment,
    DataTypeMap.MultiDepartment,
  ];
  return formCards.filter((it) => list.includes(it.dataType));
};

export const GetAllCard = (formCards: FormCard[]) => {
  if (!formCards?.length) return [];
  const list = [...formCards];
  formCards.forEach((it) => {
    if (it.columns?.length) list.push(...it.columns);
  });
  return list;
};

export const GetCardCode = (
  formCards: FormCard[],
  card: FormCard | null = null,
) => {
  const cardKeys = GetAllCard(formCards).map((card) => card.cardKey);
  if (card && card.group === 'system') {
    if (cardKeys.includes(card.cardKey)) {
      return card.cardCode + '_copy';
    }
    return card.cardCode;
  }
  if (!formCards?.length) return 'F000001';
  const codes = GetAllCard(formCards).map((card) => card.cardCode);
  const codeFilter = codes.filter((code: string) => code.indexOf('F') === 0);
  const codeNumber = codeFilter.map((code: string) =>
    parseInt(code.substring(1)),
  );
  let codeMax = Math.max(...codeNumber);
  if (!isFinite(codeMax)) codeMax = 1;
  codeMax = codeMax + 1;
  const codeStr = (codeMax + '').padStart(6, '0');
  return `F${codeStr}`;
};

export const GetCardValue = (
  formCards: FormCard[],
  curBizObject: { [key: string]: any },
) => {
  if (!formCards?.length) return {};
  const values = {};
  const list = GetAllCard(formCards).filter(
    (it) => !['layout'].includes(it.group),
  );
  list.forEach((card) => {
    const ctlValue = card.controls.ctlValue;
    let val = ctlValue?.value ?? ctlValue?.defaultValue ?? null;
    if (card.group === 'system') {
      val = curBizObject[card.cardCode] ?? null;
    }
    Object.assign(values, { [card.cardCode]: val });
  });
  return values;
};

export const FormatCardValue = (card: FormCard, val: any) => {
  const { cardKey, controls } = card;
  const dataFormat: any = controls.dataFormat;
  if (!dataFormat || [null, undefined, ''].includes(val)) return val;
  switch (cardKey) {
    case 'formDatetime':
      return dayjs(val).format(dataFormat.defaultValue);
    case 'formNumber':
      const { decimals, isShowDecimal, isShowThousand, type } =
        dataFormat.defaultValue;
      return FormatNumberValue(
        isShowDecimal,
        isShowThousand,
        decimals,
        val,
        type,
      );
    default:
      return val;
  }
};

export const FormatNumberValue = (
  isDecimal: boolean,
  isThousand: boolean,
  decimals: number,
  value: number,
  type: string,
) => {
  let val: any = value;
  if ([null, undefined, ''].includes(val)) return val;
  if (type === 'data') {
    if (isDecimal) {
      val = Number(value).toFixed(decimals);
    }
    if (isThousand) {
      return String(val).replace(/(?<!\.\d*)\B(?=(\d{3})+(?!\d))/g, ',');
    }
    return val;
  } else {
    // 百分比
    if (isDecimal) {
      return `${Number(val).toFixed(decimals)}%`;
    }
    return `${Number(val)}%`;
  }
};

export const IsEmailAvailable = (str: string) => {
  const reg =
    /^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
  if (reg.test(str)) return true;
  return false;
};

export const IsIDCardAvailable = (str: string) => {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if (reg.test(str)) return true;
  return false;
};

export const IsPhoneAvailable = (str: string) => {
  const isPhone = /^1[3456789]\d{9}$/;
  const isMobile = /^((0\d{2,3})-)?(\d{7,8})$/;
  if (isPhone.test(str) || isMobile.test(str)) return true;
  return false;
};
